package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 百度百家
 * @author KK
 *
 */
public class BaiduBaijiaAnalyse implements AnalyseNews{


	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://.*.baidu.com/article/[0-9]+",
				};
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		String html = urlMeta.getHtml();
		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		String title = null;
		String content = null;
		String author=null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;
		Integer deliverNum=0;
		Document doc = Jsoup.parse(html);
		title =doc.select("title").text();
		if (title!=null && title.contains("-"))title=StringUtils.substringBefore(title, "-");
		author = doc.select(".article-author-time a").text();
		blog.setAuthor(author);
		content = HtmlCleaner.getContentHtml(url, doc.select("#page blockquote,.article-detail"));
		date = DateUtils.matchDate(doc.select(".time").text());
		blog.setTitle(StringUtils.trimSpace(title));
		blog.setContent(content);
		blog.setDate(date);
//		deliverNum = NumberUtils.matchNumber(doc.select("a.shareCount").text());
		blog.setClickNum(clickNum);
		blog.setCommentNum(commentNum);
		//blog.setDeliverNum(deliverNum);
		blog.setUpdateUrl(url);
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url = "http://chenfang.baijia.baidu.com/article/9979";
		BaiduBaijiaAnalyse test = new BaiduBaijiaAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}

	
}
